﻿using Model;
using System;
using System.Linq;
using 纺织贸易管理系统;
namespace Update
{
    class Program
    {
        static void Main()
        {
            Connect.Environmen = "公司";
            Connect.CreatConnectstring();
            var db = Connect.GetSugar();
            Connect.dbcontext = Connect.GetSugar();
            Console.WriteLine("正在检查数据结构");
            Connect.CreatConnect().CheckAllField(new danjumingxitable());//检查单据明细表
            Connect.CreatConnect().CheckAllField(new DanjuTable());//检查单据表
            Connect.CreatConnect().CheckAllField(new StockTable());//检查库存表
            Connect.CreatConnect().CheckAllField(new LwDetail());//检查来往明细
            Connect.CreatConnect().CheckAllField(new RukuTable());//检查垃圾桶表
            Connect.CreatConnect().CheckAllField(new JuanHaoTable());//检查卷号表
            Connect.CreatConnect().CheckAllField(new DeleteLog());//检查删除日志表
            Connect.CreatConnect().CheckAllField(new ManualCode());//检查手工码单表
            Connect.CreatConnect().CheckAllField(new AccessoriesInfo());//检查辅料表
            Connect.CreatConnect().CheckAllField(new Liuzhuancard());//检查流转卡
            Connect.CreatConnect().CheckAllField(new ProofingRequirements());//检查打样单工艺要求
            Connect.CreatConnect().CheckAllField(new RawMaterial ());//检查原料
            Connect.CreatConnect().CheckAllField(new ImportAndExportOfRawMaterials());//检查进出明细
            Connect.CreatConnect().CheckAllField(new CustomerComplaints());//检查进出明细
            Connect.CreatConnect().CheckAllField(new CustomerComplaintsDetail());//检查进出明细
            Connect.CreatConnect().CheckAllField(new LXR());//检查联系人
            Connect.CreatConnect().CheckAllField(new TestData());
            Console.WriteLine("检查完毕");
            创建复合明细表.CreatTable();
            try
            {
                Console.WriteLine("创建单号表");
                Connect.CreatConnect().DoSQL("CREATE TABLE [dbo].[OddNumbers]" +
                    "( [ID][int] IDENTITY(1, 1) NOT NULL, [Document_Type][nvarchar](50) NULL,[M_Date][date] NULL," +
                    " [Odd_Numbers][int] NULL,CONSTRAINT[PK_OddNumbers]" +
                    " PRIMARY KEY CLUSTERED( [ID] ASC)WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF," +
                    " IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY]) ON[PRIMARY]");
                Console.WriteLine("创建单号表成功");
            }
            catch (Exception ex)
            {
                Console.WriteLine("创建单号表的时候发生了错误：" + ex.Message);
            }
            try
            {
                Console.WriteLine("创建入库细码表");
                Connect.CreatConnect().DoSQL("CREATE TABLE [dbo].[WarehousingDetailCode]" +
                    "( [ID][int] IDENTITY(1, 1) NOT NULL, [DocNum][nvarchar](50) NULL,[LotNum][nvarchar](50) NULL," +
                    " [Length][nvarchar](50) NULL " +
                    " PRIMARY KEY CLUSTERED( [ID] ASC)WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF," +
                    " IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY]) ON[PRIMARY]");
                Console.WriteLine("创建入库细码表成功");
            }
            catch (Exception ex)
            {
                Console.WriteLine("创建入库细码表的时候发生了错误：" + ex.Message);
            }
            try
            {
                Console.WriteLine("创建原料信息表");
                
                db.CodeFirst.SetStringDefaultLength(500/*设置varchar默认长度为200*/).InitTables(typeof(RawMaterial));
                Console.WriteLine("创建入库细码表成功");
                Console.WriteLine("创建辅料单据表");
                db.CodeFirst.SetStringDefaultLength(500/*设置varchar默认长度为200*/).InitTables(typeof(AccessoriesInfo));
                Console.WriteLine("创建辅料单据表成功");
                Console.WriteLine("创建原料单据表");
                db.CodeFirst.SetStringDefaultLength(500/*设置varchar默认长度为200*/).InitTables(typeof(ImportAndExportOfRawMaterials));
                Console.WriteLine("创建原料单据表成功");
               
           
            }
            catch (Exception ex)
            {
                Console.WriteLine("创建表的时候发生了错误：" + ex.Message);
            }
            if (db.Queryable<MenuTable>().First(x => x.FormName == "原料信息") == null)
            {

                var kcconnect = new SQLHelper.SQLHelper("Server=softkc.cn,1433;uid=sa;pwd=Kc123456;database=FinishDB");
                UpdateHelper.AddMenu(new MenuTable() { FormName = "原料信息", FatherMenu = "基本资料", MenuName = "原料信息" });
                UpdateHelper.AddMenu(new MenuTable() { FormName = "原料库存", FatherMenu = "库存管理", MenuName = "原料库存" });
                UpdateHelper.AddMenu(new MenuTable() { FormName = "原料入库列表", FatherMenu = "采购管理", MenuName = "原料入库单" });
                UpdateHelper.AddColumn("原料信息",string.Empty);
                UpdateHelper.AddColumn("原料库存", string.Empty);
                UpdateHelper.AddColumn("原料选择", string.Empty);
                UpdateHelper.AddColumn("原料入库列表", string.Empty);
                UpdateHelper.AddColumn("销售发货单", "GridView3");
                UpdateHelper.AddColumn("销售发货单", "GridView4");
            }
            try
            {
                if (db.Queryable<MenuTable>().First(x => x.FormName == "原料领用") == null)
                {
                    UpdateHelper.AddMenu(new MenuTable() { FormName = "原料领用列表", FatherMenu = "生产管理", MenuName = "原料领用单" });
                    UpdateHelper.AddColumn("原料领用列表", "gridView1");
                    UpdateHelper.AddColumn("原料领用单", "gridView1");
                    Console.WriteLine("创建原料领用菜单成功");
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("创建原料领用单的时候发送了错误:"+ex.Message );
            }
            try
            {
                //if (db.Queryable<MenuTable>().First(x => x.FormName == "细码库存") == null)
                //{
                    UpdateHelper.AddMenu(new MenuTable() { FormName = "细码库存", FatherMenu = "库存管理", MenuName = "细码库存" });
                    UpdateHelper.AddColumn("细码库存", "gridView2");
                    Console.WriteLine("创建细码库存菜单成功");
                //}
            }
            catch (Exception ex)
            {
                Console.WriteLine("创建细码的时候发送了错误:" + ex.Message);
            }
            try
            {
                //if (db.Queryable<MenuTable>().First(x => x.FormName == "细码库存") == null)
                //{
                //Connect.CreatConnect().CheckAllField(new JuanHaoTable());//检查卷号表
                var juans = db.Queryable<JuanHaoTable>().Where(x => string.IsNullOrWhiteSpace(x.ReceiptNo)).ToList();
                if (juans.Count > 0)
                {
                    if (juans.Select(x => x.ReceiptNo).Count() == 0)
                    {
                        Connect.CreatConnect().DoSQL("UPDATE JuanHaoTable set state =1 WHERE Ckmc NOT IN (select ID  from StockTable st) and state =0");
                        Connect.CreatConnect().DoSQL("UPDATE JuanHaoTable set ReceiptNo =(select rkdh from StockTable where id=juanhaotable.Ckmc) WHERE state =0");
                        Console.WriteLine("细码增加入库单号");
                    }
                }
                //}
            }
            catch (Exception ex)
            {
                Console.WriteLine("创建细码的入库单号的时候发送了错误:" + ex.Message);
            }
            try
            {
                //if (db.Queryable<MenuTable>().First(x => x.FormName == "细码库存") == null)
                //{
                UpdateHelper.AddMenu(new MenuTable() { FormName = "原料盘盈列表", FatherMenu = "库存管理", MenuName = "原料盘盈入库" });
                UpdateHelper.AddMenu(new MenuTable() { FormName = "原料盘亏列表", FatherMenu = "库存管理", MenuName = "原料盘亏出库" });
                UpdateHelper.AddMenu(new MenuTable() { FormName = "原料采购退货列表", FatherMenu = "采购管理", MenuName = "原料采购退货" });
                UpdateHelper.AddColumn("原料盘盈入库列表", "gridView1");
                UpdateHelper.AddColumn("原料盘亏出库列表", "gridView1");
                UpdateHelper.AddColumn("原料采购退货列表", "gridView1");
                UpdateHelper.AddColumn("原料盘盈单", "gridView1");
                UpdateHelper.AddColumn("原料盘亏单", "gridView1");
                UpdateHelper.AddColumn("原料退货单", "gridView1");

                //}
            }
            catch (Exception ex)
            {
                Console.WriteLine("数据库更新到1.0.2.13的时候发送了错误:" + ex.Message);
            }
            try
            {
                //if (db.Queryable<MenuTable>().First(x => x.FormName == "细码库存") == null)
                //{
              if( VersionHelper.GetCurrentVersion ()==Version.Parse ("1.0.2.13"))
                {
                    var lxrs=Connect.dbcontext.Queryable<LXR>().Where (x=>x.LX=="供货商"&&x.JE <0).ToList ();
                    foreach(var lxr in lxrs)
                    {
                        lxr.JE =0-lxr.JE;
                    }
                    Connect.dbcontext.Updateable(lxrs).ExecuteCommand();
                }
                //}
            }
            catch (Exception ex)
            {
                Console.WriteLine("数据库更新到1.0.2.13的时候发送了错误:" + ex.Message);
            }
            try
            {
                //if (db.Queryable<MenuTable>().First(x => x.FormName == "细码库存") == null)
                //{
                Connect.dbcontext.Deleteable<MenuTable>().Where(x => x.FormName == "期初建账").ExecuteCommand ();
                //UpdateHelper.AddMenu(new MenuTable() { FormName = "期初建账", FatherMenu = "系统信息", MenuName = "期初建账" });
                //}
            }
            catch (Exception ex)
            {
                Console.WriteLine("数据库更新到1.0.2.13的时候发送了错误:" + ex.Message);
            }
            try
            {
                //if (db.Queryable<MenuTable>().First(x => x.FormName == "细码库存") == null)
                //{
                if (VersionHelper.GetCurrentVersion() == Version.Parse("0.5.85"))
                {
                    var raws = Connect.dbcontext.Queryable<RawMaterial>().ToList();
                    foreach(var raw in raws )
                    {
                        raw.CanUsedQuantity = raw.InventoryQuantity;
                    }
                    Connect.dbcontext.Updateable(raws).ExecuteCommand();
                    Connect.dbcontext.Deleteable<ColumnTable>().Where (x=>x.GridName == "uiDataGridView1"&&x.FormName== "流程卡查询").ExecuteCommand();
                    var users = Connect.dbcontext.Queryable<Yhb>().ToList();
                    var kcconnect = new SQLHelper.SQLHelper("Server=softkc.cn;uid=sa;pwd=Kc123456;database=FinishDB");
                    var columns = kcconnect.Query<ColumnTable>(x => x.GridName == "uiDataGridView1" && x.FormName == "流程卡查询" && x.UserID == "10001");
                    foreach (var user in users )
                    {
                        foreach (var item in columns)
                        {
                            item.UserID = user.YHBH;
                        }
                        Connect.dbcontext.Insertable(columns);
                    }
                    columns = kcconnect.Query<ColumnTable>(x => x.GridName == "Gridview1" && x.FormName == "打卷报表" && x.UserID == "10001");
                    foreach (var user in users)
                    {
                        foreach (var item in columns)
                        {
                            item.UserID = user.YHBH;
                        }
                        Connect.dbcontext.Insertable(columns);
                    }
                }
                //}
            }
            catch (Exception ex)
            {
                Console.WriteLine("数据库更新到0.5.85的时候发送了错误:" + ex.Message);
            }
            try
            {
                //if (db.Queryable<MenuTable>().First(x => x.FormName == "细码库存") == null)
                //{
                UpdateHelper.AddMenu(new MenuTable() { FormName = "原料销售列表", FatherMenu = "销售管理", MenuName = "原料销售单" });
                UpdateHelper.AddMenu(new MenuTable() { FormName = "辅料销售列表", FatherMenu = "销售管理", MenuName = "辅料销售单" });
                UpdateHelper.AddMenu(new MenuTable() { FormName = "销售客诉列表", FatherMenu = "销售管理", MenuName = "销售客诉单" });
                //}
            }
            catch (Exception ex)
            {
                Console.WriteLine("增加销售客诉单的时候发生了错误:" + ex.Message);
            }
            try
            {
                //if (db.Queryable<MenuTable>().First(x => x.FormName == "细码库存") == null)
                //{
                UpdateHelper.AddMenu(new MenuTable() { FormName = "唛头关联", FatherMenu = "基本资料", MenuName = "唛头管理" });            
                //}
            }
            catch (Exception ex)
            {
                Console.WriteLine("增加唛头管理菜单的时候发生了错误:" + ex.Message);
            }
            VersionHelper.IncreaseVersion();//增加版本号
            //SettingService.GetSetting(x=>x.settingname== "默认仓库").settingValue;
            Console.WriteLine("更新数据库完成，按任意键退出");
            Console.ReadKey();
        }
    }
}
